package com.mnsq.common.bean.condition;

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周俭
 */
@Data
@Accessors(chain = true)
public abstract class DocumentCondition<T> {
    /**
     * action type
     */
    private Type type;
    /**
     * table name
     */
    private String name;
    /**
     * page params
     */
    private PageParams pageParams;
    /**
     * the require column
     */
    private List<String> queryColumns = new ArrayList<>();
    /**
     * the condition of query
     */
    private Map<String, String> queryParams = new HashMap<>();
    /**
     * the update info
     */
    private Map<String, String> updateParams = new HashMap<>();

    /**
     * get the  class of query object
     */
    public abstract Class<T> getDoc();

    public void updateValid() {
        Assert.notEmpty(this.updateParams, "需要修改的参数不能为空");
        this.commonValid();
    }

    public void detailValid() {
        Assert.notEmpty(this.queryParams, "查询参数不能为空");
        this.commonValid();
    }

    public void pageValid() {
        this.commonValid();
    }

    private void commonValid() {
        Assert.notEmpty(this.queryColumns, "查询对象字段不能为空");
    }

    @Data
    public static class PageParams {
        private int pageNum = 1;
        private int pageSize = 20;
        private Map<String, Integer> sortParams;
    }

    public enum Type {
        PAGE,
        LIST,
        DETAIL,
        UPDATE
    }
}
